<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/8/3
 * Time: 10:51
 */

namespace app\logic\index\withdraw;



use app\logic\BaseLogic;
use app\model\user\UserIncomeFlowModel;
use app\model\withdraw\TransferOrderModel;
use app\model\withdraw\TransferOrderNotifyModel;
use app\model\withdraw\UserWithdrawModel;

class TransferLogic extends BaseLogic
{
    protected $transferModel;

    protected static $instance;

    public function __construct()
    {
        parent::__construct();
        $this->transferModel = TransferOrderModel::getInstance();
    }

    public static function getInstance()
    {
//        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return  new self();
    }

    public function transferNotify($param)
    {
        TransferOrderNotifyModel::getInstance()->saveData($param);

        $param = $param['data'];
        $len = mb_strlen($param['order_id']);
        $orderId = $len>10 ? substr($param['order_id'],10,8) : $param['order_id'];
        $order = UserWithdrawModel::getInstance()->getWhere(['withdrawsn'=>$orderId])->first();
        if($order==false){
            return false;
        }

        switch ($param['status']) {
            case "1":
                // 支付成功（对于支付宝和微信支付是最终状态，对于银行卡大部分情况是终态，小概率会出现"退汇现象"，状态由"成功"变为"退汇"）
                // TODO 更新业务订单状态,提示用户提现成功
                $order->status = 3;
                $order->yun_status = 1;
                $order->transfered_time = $param['finished_time'];
                $order->save();
                break;
            case "2":
                // 支付失败（最终状态）
                // TODO 更新业务订单状态,提示用户提现失败，若有用户钱包体系，则需将提现金额退回至用户钱包
                $order->status = 4;
                $order->yun_status = 2;
                $order->transfer_fail = $param['status_message'].'_'.($param['status_detail_message']??'');
                $order->transfered_time = $param['finished_time'];
                $order->save();

                //失败要回退提现金额
                UserIncomeFlowModel::getInstance()->getWhere(['source_table'=>'video_user_withdraw','sourceid'=>$order->id])->delete();
                break;
            case "4":
                // 订单挂单（中间状态，挂单原因会在订单详细状态信息返回）
                // TODO 提示用户提现中，其他逻辑如：若因余额不足导致的挂单，可通知财务及时充值，72小时内补足余额后可自动继续支付
                $order->status = 4;
                $order->yun_status = 4;
                $order->transfer_fail = $param['status_message'].'_'.($param['status_detail_message']??'');
                $order->transfered_time = $param['finished_time'];
                $order->save();
                break;
            case "9":
                // 退汇，（最终状态，银行卡渠道特有现象，会先收到"成功"回调，然后再收到"退汇"的回调，一般以成功状态间隔24小时以上）
                // TODO 更新业务订单状态为“退汇”（失败），通知用户提现失败，建议用户更换其他银行卡提现，若有用户钱包体系，则需将提现金额退回至用户钱包
                $order->status = 4;
                $order->yun_status = 9;
                $order->transfer_fail = $param['status_message'].'_'.($param['status_detail_message']??'');
                $order->transfered_time = $param['finished_time'];
                $order->save();

                //失败要回退提现金额
                UserIncomeFlowModel::getInstance()->getWhere(['source_table'=>'video_user_withdraw','sourceid'=>$order->id])->delete();
                break;
            case "15":
                // 订单取消，（最终状态，只有挂单的订单才可以取消）
                // TODO 更新业务订单状态为“取消”（失败），通知用户提现失败，若有用户钱包体系，则需将提现金额退回至用户钱包
                $order->status = 4;
                $order->yun_status = 15;
                $order->transfer_fail = $param['status_message'].'_'.($param['status_detail_message']??'');
                $order->transfered_time = $param['finished_time'];
                $order->save();

                //失败要回退提现金额
                UserIncomeFlowModel::getInstance()->getWhere(['source_table'=>'video_user_withdraw','sourceid'=>$order->id])->delete();
                break;
        }

        return $order;
    }

}